import $ from 'jquery';
import { Message, Loading } from "element-ui";
import Bus from "./bus.js";
import Router from "../router"

//获取authorization
export function getAuthorization() {
	let authorization = localStorage.getItem("wisdomAuthorization");
	return authorization;
}

//启用按钮
export function enabledBtn(obj) {
	$(obj).removeAttr("disabled");
	$(obj).removeClass("is-disabled");
}

//禁用按钮
export function disabledBtn(obj) {
	$(obj).attr("disabled", true);
	$(obj).addClass("is-disabled");
}

//组装菜单列表
export function assemblyMenuData(list, id) {
	//根据传入的item设置其下children属性
	const ergodicChild = (item) => {
		let child = list.filter((i => i.parentId === item.pkSysMenu)); //查出子菜单
		if(child.length !== 0) {
			item["children"] = child; //把子菜单添加到上一级菜单
			child.map((item) => {
				return ergodicChild(item)
			});
		}
	};
	const ergodic = () => {
		//获取一个分类数组
		let subClass = list.filter((item) => {
			if(id){
				return item.parentId == id
			}else{
				return !item.parentId
			}
		});
		if(!subClass) {
			return;
		}

		// //遍历父类分类数组
		subClass.map((item) => {
			return ergodicChild(item);
		});
		return subClass;
	};
	let record = ergodic(); //(0显示所有,1显示下面的所有分类)只获取二级菜单，一级只有一个还要展开，麻烦！
	return record;
}

//组装菜单列表 倒序只获取父级
export function assemblyMenuParentData(list, id) {
	let arr = [];
	//根据传入的item设置其下children属性
	const ergodicChild = (item) => {
		let child = list.filter((i => i.pkSysMenu === item.parentId)); //查出子菜单
		if(child.length !== 0) {
			//    item["children"]=child;//把子菜单添加到上一级菜单
			child.map((item) => {
				arr.push(item);
				return ergodicChild(item)
			});
		}
	};
	const ergodic = () => {
		//获取一个分类数组
		let subClass = list.filter((item) => {
			return item.pkSysMenu == id
		});
		if(!subClass) {
			return;
		}

		// //遍历父类分类数组
		subClass.map((item) => {
			arr.push(item);
			return ergodicChild(item);
		});
		return subClass;
	};
	let record = ergodic(); //(0显示所有,1显示下面的所有分类)只获取二级菜单，一级只有一个还要展开，麻烦！
	return arr;
}

//组装部门列表
export function assemblyDepartData(list) {
	//根据传入的item设置其下children属性
	const ergodicChild = (item) => {
		let child = list.filter((i => i.parentId === item.pkSysDepartment)); //查出子菜单
		if(child.length !== 0) {
			item["children"] = child; //把子菜单添加到上一级菜单
			child.map((item) => {
				return ergodicChild(item)
			});
		}
	};
	const ergodic = () => {
		//获取一个分类数组
		let subClass = list.filter((item) => {
			return !item.parentId;
		});
		if(!subClass) {
			return;
		}

		// //遍历父类分类数组
		subClass.map((item) => {
			return ergodicChild(item);
		});
		return subClass;
	};
	let record = ergodic(); //(0显示所有,1显示下面的所有分类)只获取二级菜单，一级只有一个还要展开，麻烦！
	return record;
}

//组装流程角色列表
export function assemblyProcessRoleData(list) {
	//根据传入的item设置其下children属性
	const ergodicChild = (item) => {
		let child = list.filter((i => i.parent === item.pkProcessDetailed)); //查出子菜单
		if(child.length !== 0) {
			item["children"] = child; //把子菜单添加到上一级菜单
			child.map((item) => {
				return ergodicChild(item)
			});
		}
	};
	const ergodic = () => {
		//获取一个分类数组
		let subClass = list.filter((item) => {
			return item.parent == 0;
		});
		if(!subClass) {
			return;
		}

		// //遍历父类分类数组
		subClass.map((item) => {
			return ergodicChild(item);
		});
		return subClass;
	};
	let record = ergodic(); //(0显示所有,1显示下面的所有分类)只获取二级菜单，一级只有一个还要展开，麻烦！
	return record;
}

//转换时间格式 YYYY-MM-DD
export function dateToFormatFull(time) {
	if(time != null) {
		let dateee = new Date(time);
		let month = dateee.getMonth() + 1;
		if(month < 10) {
			month = "0" + month;
		}
		let day = dateee.getDate();
		if(day < 10) {
			day = "0" + day;
		}
		let date = dateee.getFullYear() + "-" + month + "-" + day;
		return date;
	}
	return undefined
}

//转换时间格式 YYYY-MM
export function dateToFormatMonth(time) {
	if(time != null) {
		let dateee = new Date(time);
		let month = dateee.getMonth() + 1;
		if(month < 10) {
			month = "0" + month;
		}
		let day = dateee.getDate();
		if(day < 10) {
			day = "0" + day;
		}
		let date = dateee.getFullYear() + "-" + month;
		return date;
	}
	return undefined
}

//数组去重
export function unique(ary) {
	let newAry = [];
	for(let i = 0; i < ary.length; i++) {
		if(newAry.indexOf(ary[i]) === -1) {
			newAry.push(ary[i]);
		}
	}
	return newAry;
}

//转换时间格式 YYYY-MM-DD HH:MM:SS
export function dateToFormat(time) {
	if(time != null) {
		let dateee = new Date(time).toJSON();
		let date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
		return date;
	}
	return undefined
}

//转换时间格式 YYYY-MM
export function dateToFormatYearMonth(time) {
	let currDate = new Date(time);
	let year = currDate.getFullYear();
	let month = currDate.getMonth() + 1;
	if(month < 10){
		month = "0" + month;
	}
	let arr = [];
	arr.push(year);
	arr.push(month);
	return arr;
}

//转换时间格式 YYYY
export function dateToFormatYear(time) {
	let currDate = new Date(time);
	let year = currDate.getFullYear();
	return year;
}

//处理请求结果
export function dealResult(data) {
	let result = data.result;
	let description = data.description;
	let errorMsg = data.errorMsg;
	switch(result) {
		case "0":
			if(errorMsg){
				Message.error(errorMsg);
			}else{
				if(description == "登录已失效，请重新登录") {
					Message.closeAll();
					Message.error(description);
					localStorage.clear();
					Router.push("/login");
				} else {
					Message.error(description);
				}
			}
			break;
	}
}

//开始时间不能大于等于结束时间
export function checkDate(startDate, endDate) {
	var startDateTemp = new Date(startDate).getTime();
	var endDateTemp = new Date(endDate).getTime();
	if(startDateTemp > endDateTemp || startDateTemp == endDateTemp) {
		return true;
	} else {
		return false;
	}
}

export function getMuLinUser(){
	let user = localStorage.getItem("muLinUser");
	if(user){
		user = JSON.parse(user);
	}
	return user;
}

//获取当前日期
export function initCurrDate() {
	let currDate = new Date();
	let year = currDate.getFullYear();
	let month = currDate.getMonth() + 1;
	if(month < 10) {
		month = "0" + month;
	}
	let day = currDate.getDate();
	if(day < 10) {
		day = "0" + day;
	}
	let startTime = dateToFormatFull(new Date());
	return startTime;
}

//获取当前日期对应的下月日期
export function initLastDate(date) {
	var arr = date.split('-');
	var year = arr[0]; //获取当前日期的年份
	var month = arr[1]; //获取当前日期的月份
	var day = arr[2]; //获取当前日期的日
	var days = new Date(year, month, 0);
	days = days.getDate(); //获取当前日期中的月的天数
	var year2 = year;
	var month2 = parseInt(month) + 1;
	if(month2 == 13) {
		year2 = parseInt(year2) + 1;
		month2 = 1;
	}
	var day2 = day;
	var days2 = new Date(year2, month2, 0);
	days2 = days2.getDate();
	if(day2 > days2) {
		day2 = days2;
	}
	if(month2 < 10) {
		month2 = '0' + month2;
	}

	var t2 = year2 + '-' + month2 + '-' + day2;
	return t2;
}